*** Set paths
local dir "..."
local dropbox_data "..."
local dropbox_data_create "..."
local local_data "..."
local dropbox_fig "..."
local dropbox_tab "..."


************************************************************
***LOAD DATA
************************************************************

use "`local_data'/data_base_bills_clients_matlab_2022b.dta", clear

drop if gleif_related=="Y" 
keep if trackclient!="no" 
	*NOTE: 
	*This is not a sample restriction because the fixed-effect regression would 
	*through these out if the anonymous LEI's had all different IDs


*Keep permanent investors or switchers from institutional to retail 
keep if (N_switches_IR==1 & N_switches==1) | N_switches==0 
	*NOTE:
	*the first group identifies the fixed effect
	*the second the parameter of interest. Including the few switchers from R to I here makes no difference.	
	

************************************************************
*** VARIABLES
************************************************************

drop markup markup2
gen markup   		=  (IIROC_YIELD - midyield_bl)
gen markup2 		=   markup if IIROC_SIDE=="BUY" 
replace markup2 	= - markup if IIROC_SIDE=="SELL" 

*Number of dealers
bys LEI bidderid retailer: gen a0=_n 
bys LEI a0 retailer: gen a1= _N 
bys LEI retailer: egen N_dealers2 =max(a1)
drop a0 a1 
*Number of dealers in a quarter 
gen quarter=quarter(date)
bys LEI bidderid quarter retailer: gen a0=_n 
bys LEI a0 quarter retailer: gen a1= _N 
bys LEI quarter retailer: egen N_dealers_q =max(a1)
drop a0 a1 

*Platform indicator 
gen platform=trading_venue=="CanDeal"

*Homedealer for this sample of trades
do "Homedealer.do"


************************************************************
*** TIME GRID AND DiT DUMMIES 
************************************************************

*** time grid 
tempfile temp
save  `temp' , replace

collapse IIROC_QUANTITY, by(date)
sort date 
gen time=_n 
drop IIROC_QUANTITY 

tempfile tempadd
save  `tempadd' , replace
	
use  `temp', clear 
merge m:m  date using  `tempadd'
drop _merge

*** Create treatment dummy (=1 if institutional, i.e., access at any point in the sample and 0 otherwise)
bys LEI: egen a0 = min(retailer)
gen treat = (a0==0) 
drop a0

*** Create an event dummy (=1 if institutional and 0 otherwise per time)
bys LEI date: egen a0 = max(retailer) // =1 reatiler, =0 institutional
gen event = (a0==1) // =1 if the clint is a retailer client at least once in the sample and 0 otherwise 
drop a0 

*** Create event_time (=1 in the period of the event and 0 otherwise)
	*Note: this currently only works for permanent switchers!
	
tempfile temp1
save  `temp1' , replace

collapse (max) a_switch (sum) IIROC_QUANTITY (mean)  event treat N_switches retailer, by(LEI time)
gen double a0 = time if a_switch==1
bys LEI: egen double event_h = max(a0)
drop a0

gen double event_time = time - event_h 
replace event_time =0 if N_switches==0 

*** Aggregate treatment dummy per month 
	gen time_count=.
	egen max=max(event_time)
	egen min=min(event_time)
	gen event_time_neg = - event_time 

	local tpool = 30  
	local maxing = ceil(max/`tpool')
	local mining = -ceil(min/`tpool')

	forvalues k = 1(1)`mining' {
	
	di `k'
	di `tpool'*(`k'-1) 
	di `tpool'*(`k')
	
	replace time_count=-`k' if (event_time_neg>`tpool'*(`k'-1) &  event_time_neg<=`tpool'*`k') & N_switches>0
	gen dum_`k'= (event_time_neg>`tpool'*(`k'-1) &  event_time_neg<=`tpool'*`k')*(N_switches>0)
			
	}

	forvalues k = 0(1)`maxing' {
	di `k'
	di `tpool'*`k'
	di `tpool'*(`k'+1)
		replace time_count=`k' if (event_time>=`tpool'*`k' &  event_time<`tpool'*(`k'+1)) & N_switches>0
		gen dum`k'= (event_time>=`tpool'*`k' &  event_time<`tpool'*(`k'+1))*(N_switches>0)
	}
	
	sort LEI time
	browse LEI time event_time dum* if N_switches>0 & event_time<=0

	
*Number of times traded in the time period
gen freq=1
bys LEI time_count: egen freq_m = total(freq) 	if N_switches>0
bys LEI: egen freq_tot = total(freq) 			if N_switches>0
gen share_trade = freq_m/freq_tot*100 			if N_switches>0

*quantity per month
bys LEI time_count: egen q_m = total(IIROC_QUANTITY) 	if N_switches>0

tempfile tempadd2
save  `tempadd2' , replace

use  `temp1', clear 
merge m:m LEI time using  `tempadd2'
drop _merge

	
** label
label var dum_15 "-15"
label var dum_14 "-14"
label var dum_13 "-13"
label var dum_12 "-12"
label var dum_11 "-11"
label var dum_10 "-10"
label var dum_9 "-9"
label var dum_8 "-8"
label var dum_7 "-7"
label var dum_6 "-6"
label var dum_5 "-5"
label var dum_4 "-4"
label var dum_3 "-3"
label var dum_2 "-2"
label var dum_1 "-1"
label var dum0 "0"
label var dum1 "+1"
label var dum2 "+2"
label var dum3 "+3"
label var dum4 "+4"
label var dum5 "+5"
label var dum6 "+6"
label var dum7 "+7"
label var dum8 "+8"
label var dum9 "+9"
label var dum10 "+10"
label var dum11 "+11"
label var dum12 "+12"
label var dum13 "+13"
label var dum14 "+14"
label var dum15 "+15"

****************************************************************************
* INPOT FOR THE MODEL-FIT FIGURE 9
****************************************************************************

*markup model fit  --> to compute yield drop in the data (in the model fit figure)
eststo:  reghdfe markup2 retailer  if  (N_switches==0 & retailer==1 | N_switches>0), absorb(exec_time_grid isin_id bidderid lei_id)  
eststo:  reghdfe markup2 retailer  if  (N_switches==0 & retailer==1 | N_switches>0), absorb(exec_time_grid isin_id bidderid lei_id) cl(lei_id)  
esttab using  "`dropbox_tab'/event_study_model_fit.tex",  se ar2 nobaselevels label wide  nonumbers  replace 
eststo clear

****************************************************************************
** EVENT STUDY PLOTS FIGURE 2b AND APPENDIX FIGURE A5
****************************************************************************
replace dum_1 = 0  // normalize this parameter

***** REGRESSION WITH MARKUP - IN DRAFT
xi: reghdfe markup2   dum*  if  (N_switches==0 & retailer==1 | N_switches>0), absorb(exec_time_grid isin_id lei_id bidderid)   cl(lei_id)  
estimates store Estore	

coefplot (Estore, keep(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1  dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10  mcolor(navy) ciopts(color(navy)))), xtitle(months relative to losing platform access, size(large)) ///
		 omitted baselevels  vertical drop(_cons) yline(0, lcolor(black) lpattern(dash)) ///
		 order(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10)  ///
		 graphregion(color(white))   xlabel(, angle(45) labsize(large))  yti(basis points, size(large))  ylabel(,labsize(large)) lcolor(black%70)	 
graph export "graph_event_study_markup.png",replace


***** REGRESSION WITH DAYS-TO-MATURITY- IN DRAFT
gen weeks_to_maturity_e=days_to_maturity_e/7
reghdfe weeks_to_maturity_e  dum*   if  (N_switches==0 & retailer==1 | N_switches>0), absorb( exec_time_grid  lei_id bidderid)   vce(cluster lei_id ) 
estimates store Estore	

coefplot (Estore, keep(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10   mcolor(navy) ciopts(color(navy)))) , ///
		xtitle(months relative to losing platform access, size(vlarge))  ///
		omitted baselevels  vertical drop(_cons) yline(0, lcolor(black) lpattern(dash)) ///
		order(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10)  ///
		xlabel(, angle(45) labsize(vlarge)) graphregion(color(white))   ylabel(,labsize(vlarge))  yti(weeks-to-maturity, size(vlarge)) lcolor(black%70)	
graph export "graph_event_study_trade_mat.png", replace



***** REGRESSION WITH DURATION - IN DRAFT
reghdfe duration  dum*   if  (N_switches==0 & retailer==1 | N_switches>0), absorb(exec_time_grid lei_id bidderid)   vce(cluster lei_id )  
estimates store Estore	

coefplot (Estore, keep(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10    mcolor(navy) ciopts(color(navy)))), ///
		xtitle(months relative to losing platform access,size(vlarge))  ///
		omitted baselevels  vertical drop(_cons) yline(0, lcolor(black) lpattern(dash)) ///
		order(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10) ///
		xlabel(, angle(45) labsize(vlarge)) graphregion(color(white))  ylabel(,labsize(vlarge))   yti(duration, size(vlarge)) lcolor(black%70)	
graph export "graph_event_study_trade_dur.png", replace



***** REGRESSION WITH CONVEXITY- IN DRAFT
reghdfe convexity  dum*   if  (N_switches==0 & retailer==1 | N_switches>0), absorb(exec_time_grid lei_id bidderid)   vce(cluster lei_id )  
estimates store Estore	

coefplot (Estore, keep(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10    mcolor(navy) ciopts(color(navy)))), ///
		xtitle(months relative to losing platform access,size(vlarge))  ///
		omitted baselevels  vertical drop(_cons) yline(0, lcolor(black) lpattern(dash)) ///
		order(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10) ///
		xlabel(, angle(45) labsize(vlarge)) graphregion(color(white))   xlabel(, angle(45))   ylabel(,labsize(vlarge)) yti(convextity,size(vlarge)) lcolor(black%70)
graph export "graph_event_study_trade_convex.png", replace



***** REGRESSION WITH QUANTITY 
*A) BUY AND SELL SIDE 
reghdfe IIROC_QUANTITY  dum*  if  (N_switches==0 & retailer==1 | N_switches>0), absorb(exec_time_grid  isin_id lei_id bidderid)   vce(cluster lei_id )  
estimates store Estore	

coefplot (Estore, keep(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10   mcolor(navy) ciopts(color(navy)))) , ///
	     xtitle(months relative to losing platform access, size(vlarge))  ///
		 omitted baselevels  vertical drop(_cons) yline(0, lcolor(black) lpattern(dash)) ///
		 order(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10) ///
		 xlabel(, angle(45)  labsize(vlarge)) graphregion(color(white))   yti("million C$", size(vlarge))  ylabel(,labsize(vlarge)) lcolor(black%70)	
graph export "graph_event_study_quantity.png", replace


*B) BUY SIDE 
reghdfe IIROC_QUANTITY  dum*  if  (N_switches==0 & retailer==1 | N_switches>0) & IIROC_SIDE=="BUY", absorb(exec_time_grid  isin_id lei_id bidderid)   vce(cluster lei_id )  
estimates store Estorebuy	

coefplot (Estorebuy, keep(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10   mcolor(navy) ciopts(color(navy)))) , ///
	     xtitle(months relative to losing platform access, size(vlarge))  ///
		 omitted baselevels  vertical drop(_cons) yline(0, lcolor(black) lpattern(dash)) ///
		 order(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10) ///
		 xlabel(, angle(45)  labsize(vlarge)) graphregion(color(white))   yti("million C$", size(vlarge))  ylabel(,labsize(vlarge)) lcolor(black%70)	
graph export "graph_event_study_quantity_buy.png", replace


*C) SELL SIDE 
reghdfe IIROC_QUANTITY  dum*  if  (N_switches==0 & retailer==1 | N_switches>0) & IIROC_SIDE=="SELL", absorb(exec_time_grid  isin_id lei_id bidderid)   vce(cluster lei_id )  
estimates store Estoresell	

coefplot (Estoresell, keep(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10   mcolor(navy) ciopts(color(navy)))) , ///
	     xtitle(months relative to losing platform access, size(vlarge))  ///
		 omitted baselevels  vertical drop(_cons) yline(0, lcolor(black) lpattern(dash)) ///
		 order(dum_10 dum_9 dum_8 dum_7 dum_6 dum_5 dum_4 dum_3 dum_2 dum_1 dum0 dum1 dum2 dum3 dum4 dum5 dum6 dum7 dum8 dum9 dum10) ///
		 xlabel(, angle(45)  labsize(vlarge)) graphregion(color(white))   yti("million C$", size(vlarge))  ylabel(,labsize(vlarge)) lcolor(black%70)	
graph export "graph_event_study_quantity_sell.png", replace



